from django.db import models
import constants

class Brand(models.Model):
	name = models.CharField(max_length=50, unique=True)
   	
   	class Meta:
		ordering = ["name"]
		verbose_name_plural = 'Brands'
   	
	def __unicode__(self):
		return str(self.name)

class Company(models.Model):
	name = models.CharField(max_length=50, unique=True)
	
	class Meta:
		ordering = ["name"]
		verbose_name_plural = 'Companies'
	
	def __unicode__(self):
		return str(self.name)
	
class Client(models.Model):
	title = models.CharField(max_length=1, choices=constants.TITLE_CHOICES)
	first_name = models.CharField(max_length=90)
	last_name = models.CharField(max_length=90)
	company = models.ForeignKey(Company, null=True, blank=True)
	phone_telephone = models.CharField(max_length=20, null=True, blank=True)
	phone_mobile = models.CharField(max_length=20, null=True, blank=True)
	email = models.EmailField(null=True, blank=True)
	
	@property
	def full_name(self):
		return str(self.first_name + ' ' + self.last_name)
		name.short_description = 'Name'
	
	class Meta:
		ordering = ["last_name"]

	def __unicode__(self):
		if self.company:
			return str(self.company.name)
		else:
			return str(self.full_name)
	
class Quote(models.Model):
	client = models.ForeignKey(Client)
	date = models.DateField()
	cost = models.DecimalField(max_digits=16, decimal_places=2)
	description = models.TextField(null=True, blank=True)
	
	def __unicode__(self):
		return str(self.id)	

class Job(models.Model):
	type = models.CharField(max_length=2, choices=constants.TYPE_CHOICES)	
	client = models.ForeignKey(Client)
	quote = models.ForeignKey(Quote, blank=True, null=True)
	date_start = models.DateField()
	date_end = models.DateField()
	duration = models.DecimalField(max_digits=16, decimal_places=2, help_text='In hours')
	cost_charged = models.DecimalField(max_digits=16, decimal_places=2)
	payment_received = models.BooleanField()
	payment_type = models.CharField(max_length=2, choices=constants.PAYMENT_CHOICES)
	status = models.CharField(max_length=2, choices=constants.STATUS_CHOICES)
	description = models.TextField(null=True, blank=True)
	
	@property
	def quote_estimate(self):
		try:
			return self.quote.cost
		except:
			return 0
		
	@property
	def costs(self):
		jobs = JobCost.objects.all().filter(job=self)
		
		if (len(jobs) == 0):
			return 0
		else:
			v = 0
			for job in jobs:
				v += job.cost
			return v
	
	@property
	def profit(self):
		if not self.cost_charged:
			return 0
		else:
			return self.cost_charged - self.costs
	
	def __unicode__(self):
		return str(self.id)

class Address(models.Model):
	first_line = models.CharField(max_length=25)
	second_line = models.CharField(max_length=25, blank=True, null=True)
	town = models.CharField(max_length=25, blank=True, null=True)
	city = models.CharField(max_length=25, blank=True, null=True)
	post_code = models.CharField(max_length=10)
	
	client = models.ForeignKey(Client, blank=True, null=True)
	job = models.ForeignKey(Job, blank=True, null=True)
	
	def __unicode__(self):
		return str(self.first_line)
	
class Cost(models.Model):
	reciept = models.BooleanField()
	cost = models.DecimalField(max_digits=16, decimal_places=2)
	payment_type = models.CharField(max_length=2, choices=constants.PAYMENT_CHOICES)
	date = models.DateField()
	company = models.ForeignKey(Company, null=True, blank=True)
	description = models.CharField(max_length=150, blank=True, null=True)

	def __unicode__(self):
		return str(self.id)

class JobCost(Cost):
	job = models.ForeignKey(Job)

	def __unicode__(self):
		return str(self.id)
	
class ExpenseType(models.Model):
	type = models.CharField(max_length=90, unique=True)

	class Meta:
		ordering = ["type"]
	
	def __unicode__(self):
		return str(self.type)
	
class Expense(Cost):
	type = models.ForeignKey(ExpenseType)
	

	
	def __unicode__(self):
		return str(self.id)
	
class Equipment(Cost):
	product_name = models.CharField(max_length=90)
	brand = models.ForeignKey(Brand, null=True, blank=True)
	image = models.FileField(upload_to='img/uploads/equipment/')
	
	class Meta:
		verbose_name_plural = 'Equipment'
	
	def __unicode__(self):
		return str(self.product_name)